const router=require('koa-router')();
const rendom=require('../utils/rendom');
const qs = require('querystring');
const requset=require('request');
//获取token、accid
router.post('/GetAccID',async (ctx)=>{
    let data=ctx.request.body;
    let accid=await rendom.createUUID();
    let headers=await rendom.createHeader();
    let datas={accid:accid};
    if(data.name){
        datas.name=data.name
    }
    datas=qs.stringify(datas);
    let result=await new Promise((resolve, reject) =>{
        requset({
            method:'POST',
            url:'https://api.netease.im/nimserver/user/create.action',
            headers:headers,
            body:datas
        },(error, response, body)=>{
            if(error!==null){
                resolve(error)
            }else {
                resolve(body)
            }
        })
    });
    ctx.body=result;
});

//创建聊天室
router.post('/CreateRoom',async (ctx)=>{
    let data=ctx.request.body;
    let creator=data.creator;
    let name=data.name;
    let datas={
        creator:creator,
        name:name
    };
    let headers=await rendom.createHeader();
    datas=qs.stringify(datas);
    let result=await new Promise((resolve, reject) =>{
        requset({
            method:'POST',
            url:'https://api.netease.im/nimserver/chatroom/create.action',
            headers:headers,
            body:datas
        },(error, response, body)=>{
            if(error!==null){
                resolve(error)
            }else {
                resolve(body)
            }
        })
    });
    ctx.body=result;
});

//进入聊天室
router.post('/IntoRoom',async (ctx)=>{
    let data=ctx.request.body;
    let accid=data.accid;
    let roomid=data.roomid;
    let datas={
        roomid:roomid,
        accid:accid
    };
    let headers=await rendom.createHeader();
    datas=qs.stringify(datas);
    let result=await new Promise((resolve, reject) =>{
        requset({
            method:'POST',
            url:'https://api.netease.im/nimserver/chatroom/requestAddr.action',
            headers:headers,
            body:datas
        },(error, response, body)=>{
            if(error!==null){
                resolve(error)
            }else {
                resolve(body)
            }
        })
    });
    ctx.body=result;
});

//往指定聊天室发送消息
router.post('/SendMessage',async (ctx)=>{
    let data=ctx.request.body;
    let roomid=data.roomid;
    let msgId=await rendom.createUUID();
    let fromAccid=data.fromAccid;
    let msgType=data.msgType;//0: 表示文本消息，1: 表示图片，2: 表示语音，3: 表示视频，4: 表示地理位置信息，6: 表示文件，10: 表示Tips消息，
    let datas={
        roomid:roomid,
        msgId:msgId,
        fromAccid:fromAccid,
        msgType:msgType
    };
    let headers=await rendom.createHeader();
    datas=qs.stringify(datas);
    let result=await new Promise((resolve, reject) =>{
        requset({
            method:'POST',
            url:'https://api.netease.im/nimserver/chatroom/sendMsg.action',
            headers:headers,
            body:datas
        },(error, response, body)=>{
            if(error!==null){
                resolve(error)
            }else {
                resolve(body)
            }
        })
    });
    ctx.body=result;
});

module.exports=router;